#!/bin/bash
echo "将Debian/ubuntu的系统软件（包括各种系统工具、应用程序和服务等）源更换为清华大学开源镜像站"
# 获取当前日期和时间
# 获取当前日期和时间，并用下划线替换空格
current_date=$(date "+%Y-%m-%d_%H:%M:%S")

# 输出信息
echo "软件源位置：/etc/apt/sources.list"
echo "备份原有sources.list文件到：/etc/apt/sources.list.bak.$current_date"

# 备份原有sources.list文件，并在备份文件名中加入修改时间
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak.$current_date

# 清空并替换为清华大学开源镜像站
echo "" > /etc/apt/sources.list
{
  echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free"
  echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free"

  echo ""
  echo "# Debian Bullseye Security Updates"
  echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free"
  echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free"

  echo ""
  echo "# Debian Bullseye-updates (可选)"
  echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free"
  echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free"
} | sudo tee /etc/apt/sources.list

# 更新APT缓存
echo "更换成功，测试速度——————————————————————————"
echo "更换成功，测试速度——————————————————————————"
echo "更换成功，测试速度——————————————————————————"
sudo apt update

# 输出更改源的时间
echo "Software sources updated at: $current_date"